﻿using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    [DataContract]
    public class WOffice : NotificationObject
    {
        private int officeUID;
        private String officeName;
        private int personnelUID;
        private int districtUID;
        private String position;
        private String phone;
        private WPersonnel person;

        [DataMember(Order = 0)]
        public int OfficeUID
        {
            get
            {
                return officeUID;
            }
            set
            {
                officeUID = value;
                RaisePropertyChanged("OfficeUID");
            }
        }

        [DataMember(Order = 1)]
        public String OfficeName
        {
            get
            {
                return officeName;
            }
            set
            {
                officeName = value;
                RaisePropertyChanged("OfficeName");
            }
        }

        [DataMember(Order = 2)]
        public int PersonnelUID
        {
            get
            {
                return personnelUID;
            }
            set
            {
                personnelUID = value;
                RaisePropertyChanged("PersonnelUID");
            }
        }

        [DataMember(Order = 3)]
        public int DistrictUID
        {
            get
            {
                return districtUID;
            }
            set
            {
                districtUID = value;
                RaisePropertyChanged("DistrictUID");
            }
        }

        [DataMember(Order = 4)]
        public String Position
        {
            get
            {
                return position;
            }
            set
            {
                position = value;
                RaisePropertyChanged("Position");
            }
        }

        [DataMember(Order = 5)]
        public String Phone
        {
            get
            {
                return phone;
            }
            set
            {
                phone = value;
                RaisePropertyChanged("Phone");
            }
        }

        [DataMember(Order = 6)]
        public WPersonnel Person
        {
            get
            {
                return person;
            }
            set
            {
                person = value;
                RaisePropertyChanged("Person");
            }
        }

        public WOffice()
        {
            OfficeUID = -1;
            OfficeName = "";
            PersonnelUID = -1;
            DistrictUID = -1;
            Position = "";
            Phone = "";
            Person = new WPersonnel();
        }

        public WOffice(LOffice p)
        {
            OfficeUID = p.OfficeUID;
            OfficeName = p.OfficeName;
            PersonnelUID = p.PersonnelUID;
            DistrictUID = WDistrict.GetDistrictUID(p.DistrictSelected);
            Position = p.Position;
            Phone = p.Phone;
        }

        public static int GetOfficeUID(String pOfficeName)
        {
            HttpResponse res = FridgeConnector.PostMessage(EStudy.BD_Office, EMethod.SelectAll, "");

            if (res.Status != "Success")
            {
                return -1;
            }

            List<WOffice> list = JSON.parse<List<WOffice>>(res.Data1);
            foreach (var item in list)
            {
                if (item.OfficeName.Trim() == pOfficeName.Trim())
                {
                    return item.OfficeUID;
                }
            }

            return -1;
        }

        public static List<WOffice> GetAll()
        {
            HttpResponse res = FridgeConnector.PostMessage(EStudy.BD_Office, EMethod.SelectAll, "");
            return JSON.parse<List<WOffice>>(res.Data1);
        }

        public static HttpResponse Insert(List<WOffice> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.BD_Office, EMethod.Insert, postData);
        }

        public static HttpResponse Update(List<WOffice> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.BD_Office, EMethod.Update, postData);
        }

        public static HttpResponse Delete(List<WOffice> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.BD_Office, EMethod.Delete, postData);
        }
    }
}
